package lesson4.api.helpers;

import lesson4.api.ApplicationManager;
import lesson4.pages.IndexPage;
import lesson4.pages.RegistrationPage;
import lesson4.pages.UserMenu;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class PageHelper
{
	// Logger
	private static Logger log = LoggerFactory.getLogger(PageHelper.class);
	// Application manager instance
	private final ApplicationManager app;
	// Page names
	public final static String INDEX_PAGE = "IndexPage";
	public final static String REGISTRATION_PAGE = "RegistrationPage";
	public final static String USER_MENU = "UserMenu";
	// Page objects
	private IndexPage indexPage;
	private RegistrationPage registrationPage;
	private UserMenu userMenu;

	public PageHelper(ApplicationManager p_app)
	{
		app = p_app;
	}

	public IndexPage indexPage()
	{
		app.testPage = INDEX_PAGE;

		if (indexPage == null)
		{
			indexPage = new IndexPage(app);
		}

		return indexPage;
	}

	public RegistrationPage registrationPage()
	{
		app.testPage = REGISTRATION_PAGE;

		if (registrationPage == null)
		{
			registrationPage = new RegistrationPage(app);
		}

		return registrationPage;
	}

	public UserMenu userMenu()
	{
		app.testPage = USER_MENU;
		userMenu = new UserMenu(app);

		return userMenu;
	}

	public boolean isLoggedIn()
	{
		log.info("Check for is user loggen in");
		boolean isLoggedIn = app.page().userMenu().isLinkLogoutPresent();

		return isLoggedIn;
	}

	public void logout()
	{
		if (isLoggedIn())
		{
			app.page().userMenu().logout();
		}
	}

	public boolean isRegistered()
	{
		log.info("Check for is user registered");
		boolean isRegistered = app.page().indexPage().isNotification();

		return isRegistered;
	}
}
